﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
using JustMVCTest.Models;

namespace JustMVCTest
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            
            //UNCOMMENT THE LINE BELOW TO RESET THE DB. AFTER ONE RUN- PUT IN COMMENT AGAIN
           // Database.SetInitializer<PODBContext>(new MyInitializer());
            
            //Database.SetInitializer(new DropCreateDatabaseAlways<PODBContext>());
           
            //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<PODBContext>());
            //Database.SetInitializer<PODBContext>(null);

            
        }

        public class MyInitializer : DropCreateDatabaseAlways<PODBContext>
        {
            protected override void Seed(PODBContext context)
            {
                context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX indx_papi_users_email ON PAPI_Users (email)");
                context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX indx_papi_users_requisitio ON PAPI_Users (requisitio)");
                //context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX indx_papi_users_Int_Order ON PAPI_Users (Int_Order)");
                //context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX indx_papi_users_Cost_Cent ON PAPI_Users (Cost_Cent)");

                context.Database.ExecuteSqlCommand("CREATE INDEX indx_poes_status ON POes (Status)");
                context.Database.ExecuteSqlCommand("CREATE INDEX indx_poes_DeliveryDate ON POes (DeliveryDate)");
                context.Database.ExecuteSqlCommand("CREATE INDEX indx_poes_Percentage_Update_Date ON POes (Percentage_Update_Date)");

                string createEyal =
                       @"INSERT INTO PAPI_Users 
                        (domain_username,email,first_name,last_name,requisitio,is_admin,is_superuser,is_tssc,Cost_Cent,Int_Order,filter) 
                        VALUES 
                        ('Eyal','flame2262@gmail.com','Eyal','Shaham','ShahamE',1,1,0,NULL,NULL,NULL)";

                string createRoei = 
                        @"INSERT INTO PAPI_Users 
                        (domain_username,email,first_name,last_name,requisitio,is_admin,is_superuser,is_tssc,Cost_Cent,Int_Order,filter) 
                        VALUES 
                        ('Roei','Roei7d@gmail.com','Roei','Dana','DanaR',1,1,0,NULL,NULL,NULL)";

                context.Database.ExecuteSqlCommand(createEyal);
                context.Database.ExecuteSqlCommand(createRoei);
            }
        }

    }
}